package com.hoshiibuy.dictionary.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiibuy.common.exception.ServiceException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.dictionary.entity.DicCity;
import com.hoshiibuy.dictionary.entity.param.DicCityQueryParam;
import com.hoshiibuy.dictionary.entity.param.DicCityUpdateParam;
import com.hoshiibuy.dictionary.entity.param.DicCityAddParam;
import com.hoshiibuy.dictionary.service.DicCityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import com.hoshiibuy.common.utils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  控制器
 * </p>
 *
 * @author zhangshuhu
 * @since 2019-04-28
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/city")
@Api(value = "地址管理", tags = {"地址管理"})
public class DicCityController {
    @Autowired
    private DicCityService dicCityService;

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 分页查询
    */
    @ApiOperation(value = "分页查询")
    @PostMapping("/query/page")
    public Response page(@Valid DicCityQueryParam param) {
        try {
            log.info("分页查询");
            QueryWrapper<DicCity> queryWrapper = new QueryWrapper<DicCity>();
            queryWrapper.eq(StringUtils.isNotBlank(param.getCityCode()), "city_code", param.getCityCode());
            queryWrapper.eq(StringUtils.isNotBlank(param.getName()), "name", param.getName());
            queryWrapper.eq(StringUtils.isNotBlank(param.getProvinceCode()), "province_code", param.getProvinceCode());
            queryWrapper.eq(param.getIsDel() != null, "is_del", param.getIsDel());
            IPage<DicCity> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            queryWrapper.select(BeanUtils.getSqlSelect(DicCity.class));
            return Response.data(dicCityService.page(page, queryWrapper));
          } catch (ServiceException e) {
            return Response.failed(e.getError().getMsg());
          } catch (Exception e) {
            log.error("分页查询异常", e);
            return Response.failed("分页查询异常");
        }
    }
    /**
    * @param id
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 根据id查询
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询")
    @ApiParam(name = "id", value = "主键ID", required = true)
    public Response queryById(Long id) {
        return Response.data(dicCityService.getById(id));
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 新增
    */
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public Response save(@Valid DicCityAddParam param) {
        try {
            DicCity data = BeanUtils.covertType(param, DicCity.class);
            return Response.data(dicCityService.save(data));
        } catch (Exception e) {
            log.error("新增异常", e);
            return Response.failed();
        }
    }

    /**
    * @param param
    * @return com.hoshiibuy.common.rpc.Response
    * @description: 修改
    */
    @PutMapping("/update")
    @ApiOperation(value = "修改")
    public Response update(@Valid DicCityUpdateParam param) {
        try {
            DicCity data = BeanUtils.covertType(param, DicCity.class);
            return Response.data(dicCityService.updateById(data));
        } catch (Exception e) {
            log.error("修改异常", e);
            return Response.failed();
        }
    }
}
